package com.tsxy.mall.controller.api;


import cn.hutool.core.util.StrUtil;
import com.tsxy.mall.entity.Address;
import com.tsxy.mall.entity.User;
import com.tsxy.mall.service.AddressService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.util.List;

@RestController
@RequestMapping("/api/address")
public class APIAddressController {
    @Autowired
    private AddressService addressService;
    @GetMapping("/getByUserId")
    public List getById(Integer id){
        return addressService.getByUserId(id);
    }
    @GetMapping("/getAll")
    public List getAll(){
        return addressService.getAll();
    }

    @PostMapping("/insert")
    public Integer insert(@RequestBody Address address, HttpSession session){
        User user = (User) session.getAttribute("user");
        address.setUserId(user.getId());
        if (StrUtil.isBlank(address.getCollege())  && StrUtil.isBlank(address.getMajor())&& StrUtil.isBlank(address.getPhone()) ){
            session.setAttribute("msg", "学院、专业、手机号码不能为空");
            return 0;
        }
        return  addressService.insert(address);
    }
}
